﻿using System;
using System.Collections.Generic;
using System.Text;
using TCCHWLib.Commands;
using System.Windows.Forms;


namespace TCCCompiler
{
    class Factor:IExpression
    {
        TreeNode Current_Node;
        string m_FactorType;
        bool t_for=false;
        string t_for_index;
        bool is_arr = false;
        //object m_Value;

        public string FactorType
        {
            get { return m_FactorType; }
            set { m_FactorType = value; }
        }
        //public object Value
        //{
        //    get { return m_Value; }
        //    set { m_Value = value; }
        //}
        
        public Factor(TreeNode Node,BatchCommand batch,bool T_for,string T_for_index,bool Is_arr)
        {
            Current_Node = Node;
            Current_Batch = batch;
            t_for = T_for;
            t_for_index = T_for_index;
            is_arr = Is_arr;
        }
        public BatchCommand  Get_Factor()
        {
            object index = 0;
            ParameterType index_type=ParameterType.Value;
         
            FactorType = Current_Node.Nodes[0].Text;
            Value = Current_Node.Nodes[0].Nodes[0].Text;

            switch (FactorType)
            {
                case "Constant":
                    Value = Current_Node.Nodes[0].Nodes[0].Text;
                    string str = Value.ToString();
                    if(str.Contains("'"))
                    {
                        str = str.Substring(1, str.Length - 2);
                    }
                    Value = str;
                    this.Type = ParameterType.Value;
                    break;
                case "Identifier":
                    //Current_Node = Current_Node.Nodes[0];
                    if (Current_Node.Nodes.Count > 1)
                    {
                        is_arr = true;
                        SimpleExpression exp = new SimpleExpression(Current_Node.Nodes[2].Nodes[0], Current_Batch,t_for,t_for_index,is_arr);
                        is_arr = false;
                        index = exp.Value;
                        index_type = exp.Type;

                    }
                    TxReadCommand read;
                    if(t_for && is_arr)
                        read = new TxReadCommand(t_for_index, ParameterType.Variable, 0, ParameterType.Value);
                    else
                        read = new TxReadCommand(Value, ParameterType.Variable, index, index_type);

                    Current_Batch.AddCommand(read);
                    Value = Current_Batch.CommandList.Count - 1;
                    this.Type = ParameterType.Instruction;
                    break;
                case "Simple_Expression":
                    SimpleExpression exp2 = new SimpleExpression(Current_Node.Nodes[0], Current_Batch,t_for,t_for_index,is_arr);
                    Current_Batch = exp2.Current_Batch;
                    this.Value = exp2.Value;
                    this.Type = exp2.Type;
                    break;
                case "Simple_Condition":
                    SimpleCondition condition = new SimpleCondition(Current_Node.Nodes[0], Current_Batch,t_for,t_for_index,is_arr);
                    Current_Batch = condition.Current_Batch;
                    this.Value = condition.Value;
                    this.Type = condition.Type;
                    break;
            }
            
            return Current_Batch;
        }
    }
}
